<?php
namespace jy_quanhuo;
defined('BY_JYA') or exit('error');
/*
	活动佣金计算
*/
class commission extends \plugin{
	/*
		生成分红
	*/
	function create($order){
		file_put_contents(ROOT_D.'/1.txt',$order['gamename']);
		$isfgame=in_array($order['gamename'],array('jy_merchgame_fx','jy_merchgame_bm'));
		if(!$isfgame){
			return false;
		}
		$this->order=$order;		
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$this->order['merchid'],
			'pid'=>$this->order['pid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['fee'],
			'commission_money'=>0,
			'jifen'=>0,
			'gamename'=>$this->order['gamename'],
			'createtime'=>TIMESTAMP,
		);
		$this->setting=$this->plugin_setting('basic');
		if($this->setting['become_type']){
			$topid=$this->order['pid'];
		}else{
			$topid=0;
		}
		$member=pdo_get('jy_merch_game_fromuser',array('mid'=>$this->order['mid'],'uniacid'=>$_SESSION['uniacid'],'pid'=>$topid),array('id','fromuser'));
		$fromuser=$member['fromuser'];
		if(!$fromuser){
			return false;
		}
		for($i=0;$i<2;$i++){
				if(!$fromuser){
					break;
				}
				$agent=pdo_get('jy_merch_game_fromuser',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'pid'=>$topid),array('mid','fromuser'));
				$agent['mid']=$agent['mid']?:$fromuser;
				$fromuser=$agent['fromuser'];
				$this->create_commission($agent,$i);
				//如果他的上级是我
				$ta_agent_id=pdo_getcolumn('jy_merch_game_fromuser',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'pid'=>$topid),'fromuser');
				if($ta_agent_id==$agent['id']){
					break;
				}
		}
		//生成分销订单
		$indata=$this->commission_order;
		pdo_insert('jy_merch_game_commission_order',$indata);
		pdo_update('jy_merch_game_order',array('commission_money'=>$this->commission_order['commission_money']),array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$this->order['ordersn']));
		return true;
	}
	/*
		佣金
	*/
	function create_commission($agent,$i=0){
			if($i && $this->order['gamename']!='jy_merchgame_fx'){
				return true;
			}
			$money=0;
			foreach($this->order['goods'] as $goods){
				if(!$goods['cartnum']){
					continue;
				}
				if($this->order['gamename']=='jy_merchgame_fx'){
					$money=$i?$goods['fxfee02']:$goods['fxfee01'];
				}elseif($this->order['gamename']=='jy_merchgame_bm'){
					$money=$goods['bmfee'];
				}
			}
			$this->commission_order['commission_money'] +=$money;
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$this->order['merchid'],
					'pid'=>$this->order['pid'],
					'mid'=>$agent['mid']?:$agent['id'],
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'jifen'=>0,
					'gamename'=>$this->order['gamename'],
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'level'=>$i++,
				);
				pdo_insert('jy_merch_game_commission',$new_commission);
			}
			return true;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		pdo_update('jy_merch_game_commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$order['ordersn']));
		return true;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status=0,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		pdo_update('jy_merch_game_balance',$update,array('id'=>$balance_id));
		return true;
	}
}